package sort.insertSort;

/**
 * @Author: zhang
 * @Description:越有序越快
 * @Date: Created in 17:38 2019/9/30
 */
public class Test {
    public static void main(String[] args) {
        int[] arrays = new int[]{5, 1, 7, 4, 8, 9, 2, 3, 6, 0};
        insertSort(arrays);
        for (int i = 0; i < arrays.length; i++) {
            if (i != arrays.length - 1) {
                System.out.print(arrays[i] + " ");
            } else {
                System.out.print(arrays[i]);
            }
        }
    }

    public static void insertSort(int[] a) {
        for (int i = 1; i < a.length; i++) {
            int tmp = a[i];
            int j = i;
            while (j > 0 && tmp < a[j - 1]) {
                a[j] = a[j - 1];
                j--;
            }
            if(j != i){
                a[j] = tmp;
            }
        }
    }
}
